package com.hejjon.core.bean;

import com.hejjon.core.domain.BaseResponse;
import com.hejjon.core.enumer.ResultCodeEnum;

/**
 * @author: cs
 * @date: 2024-12-20 01:43:25
 * @since: 1.0
 */
public class ResponseResult {

    private Integer code;       // 响应码

    private String msg;         // 成功信息

    private Object data;        // 数据


    public ResponseResult() {

    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     *  返回成功
     * @return
     */
    public static ResponseResult success() {
        return new ResponseResult(ResultCodeEnum.SUCCESS.getRet(), "操作成功", null);
    }


    /**
     *  返回成功数据
     * @param data
     * @return
     */
    public static ResponseResult success(Object data) {
        return new ResponseResult(ResultCodeEnum.SUCCESS.getRet(), "操作成功", data);
    }

    /**
     *  返回成功数据和信息
     * @param data
     * @return
     */
    public static ResponseResult success(String msg, Object data) {
        return new ResponseResult(ResultCodeEnum.SUCCESS.getRet(), msg, data);
    }


    public static ResponseResult error() {
        return new ResponseResult(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), "", null);
    }


    /**
     * 返回失败响应码和信息
     * @param
     * @param emsg
     * @return
     */
    public static ResponseResult error(String emsg) {
        return new ResponseResult(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), emsg, null);
    }

    /**
     * 返回失败响应码和信息
     * @param
     * @param emsg
     * @return
     */
    public static ResponseResult error(String emsg, Object data) {
        return new ResponseResult(ResultCodeEnum.INTERNAL_SERVER_ERROR.getRet(), emsg, data);
    }

    public static ResponseResult error(Integer code, String emsg, Object data) {
        return new ResponseResult(code, emsg, data);
    }

    public static ResponseResult error(Integer code, String emsg) {
        return new ResponseResult(code, emsg, null);
    }

    /**
     * 警告
     * @param msg
     * @return
     */
    public static ResponseResult warn(String msg) {
        return new ResponseResult(ResultCodeEnum.Native.getRet(), msg, null);
    }

    public static ResponseResult warn(String msg, Object data) {
        return new ResponseResult(ResultCodeEnum.Native.getRet(), msg, data);
    }

    /**
     *
     * @param data
     * @return
     */
    public ResponseResult data(Object data) {
        this.data = data;
        return this;
    }


    public static ResponseResult toResult(boolean state) {
        return state ? success() : error();
    }

    public static ResponseResult toResult(boolean state, String msg) {
        return state ? success(msg, null) : error("操作失败!");
    }

    public static ResponseResult toResult(int rows) {
        return rows > 0 ? success() : error();
    }

    public static ResponseResult toResult(int rows, String msg) {
        return rows > 0 ? success(msg, null) : error();
    }

    public static ResponseResult toResult(BaseResponse response) {
        return response.isStatus() ? success(response.getMessage(), null) : error(response.getMessage());
    }
}
